﻿using System;
using System.Reflection;
using Hallenberg.SmartClient.Interfaces;
using Hallenberg.SmartClient.IO;
using Hallenberg.SmartClient.Menu;
using NUnit.Framework;

namespace Hallenberg.SmartClient.Test
{
    [TestFixture]
    public class WorkItemControllerTest
    {
        private const string HallenbergPrefix = "Hallenberg";
        
        [Test]
        public void SmartClient_WorkItemController_Assert_Standard_Services()
        {
            WorkItemController workItem = new WorkItemController();
            workItem.Initialize();

            Assert.That(workItem.Services.Resolve<IServiceLocator>(), Is.Not.Null);
            Assert.That(workItem.Services.Resolve<WorkItemController>(), Is.Not.Null);
            Assert.That(workItem.Services.Resolve<Modules>(), Is.Not.Null);
            Assert.That(workItem.Services.Resolve<MenuManager>(), Is.Not.Null);
            Assert.That(workItem.Services.Resolve<IResourceMapper>(), Is.Not.Null);
        }

        [Test]
        public void SmartClient_WorkItemController_HandleType()
        {
            WorkItemController workItem = new WorkItemController();
            workItem.Initialize();

            workItem.HandleType(typeof(Hallenberg.SmartClient.TypeHandlers.BaseModuleHandler));
            workItem.HandleType(typeof(TestObject.Module));
        }

        [Test]
        public void SmartClient_WorkItemController_AddRegisteredAssembly_PassNull_ExpectFail()
        {
            WorkItemController workItem = new WorkItemController();
            workItem.Initialize();
            ArgumentNullException exception = Assert.Throws<ArgumentNullException>(() => workItem.AddRegisteredAssembly(null));

            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.ParamName, Is.EqualTo("assembly"));
        }

        [Test]
        public void SmartClient_WorkItemController_GetRegisteredAssemblies()
        {
            WorkItemController workItem = new WorkItemController();
            workItem.Initialize();
            workItem.GetRegisteredAssemblies();
        }

        [Test]
        public void SmartClient_WorkItemController_HandleType_With_AttributeExtenders()
        {
            WorkItemController workItem = new WorkItemController();
            workItem.AddRegisteredAssembly(Assembly.GetAssembly(typeof(TestObject.MenuItemAttributeExtender)));

            workItem.HandleType(typeof(TestObject.TestController));

            Assert.That(TestObject.MenuItemAttributeExtender.HandlerHasExecuted, Is.True);
        }

        [Test]
        public void SmartClient_WorkItemController_HandleType_With_ClassExtenders()
        {
            BaseSmartClientApplication smartClientApplication = new BaseSmartClientApplication();
            smartClientApplication.WorkItem.ModuleLoader.AutoLoadModules(smartClientApplication, new string[] { HallenbergPrefix });
            
            WorkItemController workItem = smartClientApplication.WorkItem;
            workItem.Services.RegisterType<ILocalization, Fakes.LocalizationHelper>();
            workItem.AddRegisteredAssembly(Assembly.GetAssembly(typeof(TestObject.BaseModuleHandlerExtender)));

            smartClientApplication.ApplicationStart();

            Assert.That(TestObject.BaseModuleHandlerExtender.HandlerHasExecuted, Is.True);
        }
    }
}
